﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;
using SEO.Entity;

/// <summary>
/// 新闻源操作
/// </summary>
public partial class DoNewsWebSEO : AdminPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.NewsWebSEO;
    }

    /// <summary>
    /// 提交操作
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtUrl.Text.Trim()))
        {
            Until.Alert("请输入文章网址！");
            return;
        }
        string[] urlList = txtUrl.Text.Trim().Split('\n');
        if (urlList.Length > 50)
        {
            Until.Alert("文章网址最多输入50行！");
            return;
        }
        BisNewsWeb bisNewsWeb = new BisNewsWeb();
        BisNewsWebSEO bisNewsWebSEO = new BisNewsWebSEO();
        string strUrl = string.Empty;
        List<string> PageUrlList = new List<string>();
        List<string> PageTitleList = new List<string>();
        List<int> UserList = new List<int>();
        //获取有标识的用户
        DataTable dtUser = new BisAdminInfo().GetAllListByKey();
        for (int i = 0; i < urlList.Length; i++)
        {
            //去"/"
            string strTemUrl = urlList[i].Trim();
            if (strTemUrl.Substring(strTemUrl.Length - 1, 1) == "/")
            {
                strTemUrl = strTemUrl.Substring(0, strTemUrl.Length - 1);
            }
            PageUrlList.Add(strTemUrl);
        }
        PageUrlList = PageUrlList.Distinct<string>().ToList();
        for (int i = 0; i < PageUrlList.Count; i++)
        {
            strUrl = PageUrlList[i];
            if (!string.IsNullOrEmpty(strUrl))
            {
                //验证是否新闻源网址
                string strTopDomain = Common.GetTopDomain(strUrl);
                if (bisNewsWebSEO.CheckNewsWebSEO(0, strUrl))
                {
                    Until.Alert(PageUrlList[i] + "网址已经存在。");
                    return;
                }
                if (!bisNewsWeb.CheckNewsWeb(strTopDomain))
                {
                    Until.Alert(PageUrlList[i] + "不是新闻源网址。");
                    return;
                }
                //获取新闻网标题和内容
                string strHtml = string.Empty;
                string strPageTitle = HtmlTool.GetTitleAndBody(strUrl, ref strHtml);
                if (string.IsNullOrEmpty(strHtml))
                {
                    Until.Alert(PageUrlList[i] + "访问失败。");
                    return;
                }
                //匹配用户
                int nUser = 0;
                foreach (DataRow drUser in dtUser.Rows)
                {
                    if (strHtml.IndexOf(drUser["UserKey"].ToString()) >= 0)
                    {
                        nUser = Convert.ToInt32(drUser["ID"]);
                        break;
                    }
                }
                if (nUser == 0)
                {
                    Until.Alert(PageUrlList[i] + "匹配不到用户，请确定。");
                    return;
                }
                PageTitleList.Add(strPageTitle);
                UserList.Add(nUser);
            }
        }

        bool bResult = false;
        try
        {
            //批量新增
           
            for (int i = 0; i < PageUrlList.Count; i++)
            {
                NewsWebSEO info = new NewsWebSEO();
                info.AdminID = UserList[i];
                info.PageTitle = PageTitleList[i];
                info.PageUrl = PageUrlList[i];
                bisNewsWebSEO.Insert(info);
            }
            bResult = true;
        }
        catch
        {
            bResult = false;
        }
        //结果处理
        if (bResult)
        {
            Until.AlertJS("提交成功！", "ResetPage();");
        }
        else
        {
            Until.Alert("提交失败！");
        }
        
    }
}